package com.wangsd.web.face.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wangsd.common.basicbean.Result;
import com.wangsd.common.enums.CommonStatic;
import com.wangsd.web.MyController;
import com.wangsd.web.face.entity.Device;
import com.wangsd.web.face.entity.ServiceBusiness;
import com.wangsd.web.face.entity.Store;
import com.wangsd.web.face.service.DeviceService;
import com.wangsd.web.face.service.ServiceBusinessService;
import com.wangsd.web.face.service.StoreService;
import com.wangsd.web.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.wangsd.common.enums.ResultCode.DEVICE_NO_REPEAT;
import static com.wangsd.common.enums.ResultCode.NO_ASSUPER_AUTH;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author generator
 * @since 2019-09-09
 */
@Api(value = "device", tags = "设备管理")
@RestController
@RequestMapping("/face/device")
@Slf4j
public class DeviceController extends MyController {
    @Autowired
    DeviceService deviceService;

    @Autowired
    ServiceBusinessService serviceBusinessService;
    @Autowired
    StoreService storeService;

    @ApiOperation(value = "查询设备列表", notes = "查询设备列表", httpMethod = "POST")
    @RequestMapping(value = "/selectList/{pageNum}/{pageSize}", method = RequestMethod.POST)
    public Result<IPage<Device>> selectList(@ApiParam(value = "当前页", required = true) @PathVariable("pageNum") Integer pageNum,
                                            @ApiParam(value = "每页大小", required = true) @PathVariable("pageSize") Integer pageSize,
                                            @RequestBody @ApiParam(name = "device", value = "传入json格式") Device device) {
        SysUser userInfo = getUserInfo();
        if (99 != userInfo.getType()) {
            device.setCode(userInfo.getCode());
        }
        IPage<Device> serviceBusinessIPage = deviceService.selectList(pageNum, pageSize, device);
        return Result.success(serviceBusinessIPage);
    }


    @ApiOperation(value = "新增或修改设备", notes = "新增或修改设备", httpMethod = "POST", response = Result.class)
    @RequestMapping(value = "addOrUpdate", method = RequestMethod.POST)
    public Result addOrUpdate(Device device) {
        SysUser userInfo = getUserInfo();
        if (CommonStatic.IS_SUPER_MANAGER == userInfo.getType()) {
            ServiceBusiness serviceBusiness = serviceBusinessService.getById(device.getBusinessServiceId());
            device.setCode(serviceBusiness.getCode());
        } else {
            device.setCode(userInfo.getCode());
        }
        deviceService.addOrUpdate(device);
        return Result.success();
    }

    @ApiOperation(value = "删除设备信息", notes = "删除设备信息", httpMethod = "GET", response = Result.class)
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Result delete(@ApiParam(value = "设备id", required = true) @PathVariable("id") Long id) {
        deviceService.removeById(id);
        return Result.success();
    }

    @ApiOperation(value = "通过id查询设备", notes = "通过id查询设备", httpMethod = "GET", response = Result.class)
    @GetMapping("/selectById/{id}")
    public Result<Device> selectById(@ApiParam(value = "设备id", required = true) @PathVariable Long id) {
        Device device = deviceService.getById(id);
        return Result.success(device);
    }

    @ApiOperation(value = "通过设备编号查询设备", notes = "通过设备编号查询设备", httpMethod = "GET", response = Result.class)
    @GetMapping("/selectByDeviceNo/{deviceNo}")
    public Result<Device> selectByDeviceNo(@ApiParam(value = "设备编号", required = true) @PathVariable String deviceNo) {
        Device result = deviceService.selectByDeviceNo(deviceNo);
        if (result != null) {
            return Result.message(DEVICE_NO_REPEAT);
        }
        return Result.success();
    }

    @ApiOperation(value = "通过id修改设备状态", notes = "通过id修改设备状态", httpMethod = "POST", response = Result.class)
    @PostMapping("/updateStatusById")
    public Result updateStatusById(Device device) {
        deviceService.updateById(device);
        return Result.success();
    }

    @ApiOperation(value = "查询所有未分配的设备", notes = "查询所有未分配的设备", httpMethod = "GET", response = Result.class)
    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    public Result selectAll() {
        SysUser loginUser = getUserInfo();
        List<Device> list = deviceService.selectAllByUserCode(loginUser.getCode());
        return Result.success(list);
    }
}

